package com.dailyyoga.view;

import com.tools.MultipleScreenTool;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import cn.domob.android.ads.DomobAdEventListener;
import cn.domob.android.ads.DomobAdManager.ErrorCode;
import cn.domob.android.ads.DomobAdView;

public class BannerAD {

	public static final String PUBLISHER_ID = "56OJzcJouNWkC29Zq/";
	public static final String InlinePPID_ACTION_LIB = "16TLmnFvApy1YNUH-zctm_Yk";
	public static final String InlinePPID_SESSION_PLAN = "16TLmnFvApy1YNUH-8LqpI9s";
	public static final String InlinePPID_SESSION_PLAY = "16TLmnFvApy1YNUH-i8ow94s";
	public static final String InlinePPID_SESSION_LIST = "16TLmnFvApy1YNUH-qootiwi";

	FrameLayout mAdContainer;
	DomobAdView mAdBannerView;
	Context mContext;

	public BannerAD(Context context, String inlinePPID, FrameLayout adContainer) {
		mAdContainer = adContainer;
		mContext = context;
		mAdBannerView = new DomobAdView(context, PUBLISHER_ID, inlinePPID,
				getSize());
		mAdBannerView.setKeyword("health");
		mAdBannerView.setUserGender("male");
		mAdBannerView.setUserBirthdayStr("1987-04-11");
		mAdBannerView.setUserPostcode("710075");
		mAdBannerView.setAdEventListener(new DomobAdEventListener() {

			public void onDomobAdClicked(DomobAdView arg0) {
				// TODO Auto-generated method stub
				Log.i("DomobSDKDemo", "onDomobAdClicked");
			}

			public void onDomobAdFailed(DomobAdView arg0, ErrorCode arg1) {
				// TODO Auto-generated method stub
				arg0.setVisibility(View.GONE);
				Log.i("DomobSDKDemo",
						"onDomobAdFailed : ErrorCode = " + arg1.toString());
			}

			public void onDomobAdOverlayDismissed(DomobAdView arg0) {
				arg0.setVisibility(View.GONE);
				Log.i("DomobSDKDemo", "onDomobAdOverlayDismissed");
			}

			public void onDomobAdOverlayPresented(DomobAdView arg0) {
				arg0.setVisibility(View.VISIBLE);
				Log.i("DomobSDKDemo", "onDomobAdOverlayPresented");
			}

			public Context onDomobAdRequiresCurrentContext() {
				// TODO Auto-generated method stub
				Log.i("DomobSDKDemo", "onDomobAdRequiresCurrentContext");
				return mContext;
			}

			public void onDomobAdReturned(DomobAdView arg0) {
				// TODO Auto-generated method stub
				Log.i("DomobSDKDemo", "onDomobAdReturned");
			}

			public void onDomobLeaveApplication(DomobAdView arg0) {
				// TODO Auto-generated method stub
				Log.i("DomobSDKDemo", "onDomobAdReturned");
			}

		});
	}

	public void showAd() {
		// LinearLayout layout = (LinearLayout) mAdContainer
		// .findViewById(R.id.container_layout_ad);
		//
		// if (layout.indexOfChild(mAdBannerView) == -1) {
		// layout.addView(mAdBannerView);
		// }
		//
		// mAdContainer.setVisibility(View.VISIBLE);
	}

	public void dismissAd() {
		// LinearLayout layout = (LinearLayout) mAdContainer
		// .findViewById(R.id.container_layout_ad);
		//
		// if (layout.indexOfChild(mAdBannerView) != -1) {
		//
		// layout.removeView(mAdBannerView);
		// }
		// mAdContainer.setVisibility(View.GONE);
	}

	private String getSize() {

		String size = null;
		switch (MultipleScreenTool.getScreenCategory(mContext)) {

		case smallScreens:
		case normalScreens:
			size = DomobAdView.INLINE_SIZE_320X50;
			break;
		case xlargeScreens:
			size = DomobAdView.INLINE_SIZE_728X90;
			break;
		case largeScreens:
			size = DomobAdView.INLINE_SIZE_320X50;
			break;

		default:
			break;
		}
		return size;
	}
}
